package com.yi.mall.order.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yi.mall.common.model.OrderInfo;
;
import com.yi.mall.common.model.OrderQuery;
import com.yi.mall.order.mapper.OrderInfoMapper;
import com.yi.mall.order.service.IOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author zzy
 * @since 2025-07-28
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Override
    public Page page(OrderQuery query) {
        // 构建条件构造器
        LambdaQueryChainWrapper<OrderInfo> wrapper = new LambdaQueryChainWrapper<>(baseMapper);
        // 拼接条件
        // 按用户id查询
        if(query.getUserId() != null){
            wrapper.eq(OrderInfo::getUserId, query.getUserId());
        }
        // 按订单编号查询
        if(query.getOrderId() != null){
            wrapper.eq(OrderInfo::getOrderId, query.getOrderId());
        }
        // 按订单状态查询
        if(query.getStatus() != null){
            wrapper.eq(OrderInfo::getStatus, query.getStatus());
        }
        // 按下单时间查询
        if(query.getStartTime() != null && query.getEndTime() != null){
            wrapper.between(OrderInfo::getCreateTime, query.getStartTime(), query.getEndTime());
        }else if(query.getStartTime() != null){
            wrapper.between(OrderInfo::getCreateTime, query.getStartTime(),"9999-12-31");
        }else if(query.getEndTime() != null){
            wrapper.between(OrderInfo::getCreateTime, "1970-01-01", query.getEndTime());
        }
        // 选择列
        wrapper.select(OrderInfo::getOrderId,  OrderInfo::getCreateTime , OrderInfo::getTotalAmount);
        // 分页
        Page page = new Page<>();
        if(query.getCurrent() != null){
            page.setCurrent(query.getCurrent());
        }
        if(query.getSize() != null){
            page.setSize(query.getSize());
        }
        return wrapper.page(page);
    }


}
